# -*- coding: utf-8 -*-
# בש״ד
#
# Miramar CMS for Django - Part of the Cuba Libre Project
# Copyright (C) 2009, Joshua "jag" Ginsberg <jag@flowtheory.net>
# 
# Por mi amor, que inspira a mi cada respiración y que echa de Cuba
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.contrib import admin
from django.contrib.admin.util import unquote
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _
from django import forms
from django.utils.functional import update_wrapper
from django.template import RequestContext
from django.shortcuts import render_to_response
from miramar import models
from miramar import api
from miramar import settings
from miramar.forms import PageForm


class PageAdmin(admin.ModelAdmin):
    
    def is_published(obj):
        return (obj.published_version is not None and _(u'Yes')) or _(u'No')
    is_published.short_description = _(u'Is published?')
    
    list_display = ('slug', 'title', is_published)
    search_fields = ('slug', 'title', 'published_version__body')
    form = PageForm
    
    def add_view(self, request, form_url='', extra_context=None):
        # I don't really have an idea what to do here yet. Adding a page
        # is done through the miramar_page_create view which requires a slug
        # in the url. So for now, I'm going to just redirect to changelist.
        return HttpResponseRedirect(reverse('admin:miramar_page_changelist'))
    
    def change_view(self, request, object_id, extra_context=None):
        obj = self.queryset(request).get(pk=unquote(object_id))
        slug = obj.slug
        return HttpResponseRedirect(reverse('miramar_page_update',
                                            kwargs={'slug': slug}))
    
    def changelist_view(self, request, extra_context=None):
        ctx = {'ready_to_publish': self.model.on_site.ready_to_publish(),
               'recently_updated': self.model.on_site.recently_updated(),
               'most_stale': self.model.on_site.most_stale(),
               'recent_work': self.model.on_site.recent_work(request.user)}
        return super(PageAdmin, self).changelist_view(request, extra_context=ctx)
    
    history_view = change_view
    
    def get_urls(self):
        from django.conf.urls.defaults import patterns, url

        def wrap(view):
            def wrapper(*args, **kwargs):
                return self.admin_site.admin_view(view)(*args, **kwargs)
            return update_wrapper(wrapper, view)

        info = self.model._meta.app_label, self.model._meta.module_name
        urlpatterns = patterns('', 
                               url(r'^slugentry/$',
                                   wrap(self.slugentry_view),
                                   name='%s_%s_slugentry' % info))
        urlpatterns += super(PageAdmin, self).get_urls()
        print urlpatterns
        return urlpatterns
    
    def slugentry_view(self, request, extra_context=None):
        ctx = RequestContext(request, {'jquery_urlstub': settings.JQUERY_URLSTUB})
        return render_to_response('admin/miramar/page/slugentry.html', 
                                  context_instance=ctx)
    
    class Media:
        js = (settings.JQUERY_URLSTUB, settings.SIMPLEMODAL_JS_URLSTUB)

admin.site.register(models.Page, PageAdmin)